//
//  CWJavaScriptApiFileSystem.m
//  CWDevelop
//
//  Created by LittoCats on 7/10/14.
//  Copyright (c) 2014 Littocats. All rights reserved.
//

#import "CWJavaScriptApiSystem.h"

#import "CWNetWork.h"
#import "ZipArchive.h"

#import "NSDictionary+CW.h"
#import "NSObject+CW.h"

#import "CWImageGallery.h"

#define SYSTEM action[@"system"]

@implementation CWJavaScriptApiSystem

//+ (void)analyseAction:(NSMutableDictionary *)action
//{
//    [super analyseAction:action];
//    
//}

//loadFile
CWJavascriptApi(loadFile)
{
    NSString *filePath = [CWJSCONTEXT analyzeResourcePath:params];
    NSString *fileContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    [CWJSCALLBACK evaluateWithArguments:fileContent, nil];
}
//http 请求 ，默认为post请求
CWJavascriptApi(get)
{
    if (![SYSTEM isEqualToString:@"Http"]) return;
    
    [CWNetWork get:params[@"url"] params:[params dictionaryByRemoveObjectForKey:@"url"] completionHandler:^(NSDictionary *response,
                                                                   NSError *error) {
       if (error)
           [CWJSCALLBACK evaluateWithArguments:@{@"error_num":@(error.code),@"error":error.domain,@"success":@(NO)}, nil];
        else
            [CWJSCALLBACK evaluateWithArguments:response, nil];
    }];
}

//download
CWJavascriptApi(download)
{
    if (![SYSTEM isEqualToString:@"Http"]) return;

    
#if DEBUG_ON
    __block NSString *urlString = params[@"url"];
    [CWNetWork analyseDebugServer:^(NSURL *serverURL) {
        urlString = [serverURL absoluteString];
#else
        NSString *urlString = params[@"url"];
#endif
        [CWNetWork download:urlString params:[params dictionaryByRemoveObjectForKey:@"url"]completionHandler:^(NSString *response, NSError *error) {
            if (error)
                [CWJSCALLBACK evaluateWithArguments:@{@"error_num":@(error.code),@"error":error.domain,@"success":@(NO)}, nil];
            else
                [CWJSCALLBACK evaluateWithArguments:@{@"data":response, @"success":@(YES)}, nil];
        }];
#if DEBUG_ON
    }];
#endif
}

//zip
CWJavascriptApi(unCompress){
    if (![SYSTEM isEqualToString:@"ZipArchive"]) return;
    
    NSString *zipFile = params[@"file"];
    NSString *path = [CWJSCONTEXT analyzeResourcePath:params[@"to"]];

    BOOL isDirectory;
    NSError *error;
    if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) {
        if (!isDirectory) {
            [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
            if (!error) [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
        }
    }else{
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
    }
    if (error){
        [CWJSCALLBACK evaluateWithArguments:@{@"success":@(NO)}, nil];
        return;
    }
    ZipArchive *zipArchive = [[ZipArchive alloc] init];
    if ([zipArchive UnzipOpenFile:zipFile]) {
        if ([zipArchive UnzipFileTo:path overWrite:YES]) {
            [CWJSCALLBACK evaluateWithArguments:@{@"success":@(YES),@"path":path}, nil];
            return;
        }
    }
    [CWJSCALLBACK evaluateWithArguments:@{@"success":@(NO)}, nil];
}

CWJavascriptApi(compress){
    
}

#pragma mark- showImageGallery
CWJavascriptApi(showImageGallery)
{
    [CWImageGallery popWithImages:params fromView:nil withIndex:0];
}
@end
